<template>
  <el-container v-loading="loading">
    <div class="box">
      <h3>{{activeheader}}</h3>
      <div class="input-box">
        <span></span>
        <label class="label require">分院名称:</label>
        <el-input class="input-width" v-model="name" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label require">所在地区:</label>
        <user-address :defaultValue="defaultAddress" ref="addBranchAddress" seletStyle="width:216px"/>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label require">分院地址:</label>
        <el-input class="input-width address" v-model="address" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label">营业时间:</label>
        <el-time-picker is-range v-model="workTime"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="选择时间范围">
        </el-time-picker>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label require">联系人：</label>
        <el-input class="input-width" v-model="contactPerson" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label">联系电话：</label>
        <el-input class="input-width" v-model="phone" placeholder="请输入内容"></el-input>
      </div>
      <div class="input-box">
        <span></span>
        <label class="label">上线状态：</label>
        <el-radio-group v-model="deleteFlag">
          <el-radio label = '1'>上线</el-radio>
          <el-radio label = '0'>下线</el-radio>
        </el-radio-group>
      </div>
      <div class="input-box box-last">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="cancle">取消</el-button>
      </div>
    </div>
  </el-container>
</template>

<script>
import UserAddress from "components/address";
import check from 'utils/check';
import { Dateformat, GetDate } from 'utils/formData';
let headers = ['新增分院','修改分院信息'];
let requireArray = ['name','contactPerson','address','province','city','country'];
let urls = ['/physicalChild','/chinaMedChild','/dentistryChild'];

export default {
  name: 'branchAdd',
  data() {
    return {
      name: '',
      address: '',
      workTime: '',
      contactPerson: '',
      phone:'',
      deleteFlag: '1',
      loading: false,
      activeheader: headers[0],
      check: check.methods.check,
      checkTip: check.tips.checkTip,
      defaultAddress: null, //默认地址
      code: '0',  //跳转机构类型
      codeId: '', //分院id
      id: '' //编辑id 修改
    }
  },
  mounted(){
    this.editInit();
  },
  methods:{
    editInit(){
      if( !this.$route.query ) return;      
      let { type, code, codeId, id } = this.$route.query;
      this.code = code;
      this.codeId = codeId;
      this.id = id;
      if(type === 'edit') this.activeheader = headers[1];
      if( !id ) return;

      this.loading = true;
      let url = urls[this.code];
      this.api.get(`${url}/${id}`).then((res) => { //查询分院详情
        this.dataInit(res.data)
        this.loading = false;
      }).catch((err) => {
        console.log(err)
        this.loading = false;
        this.$message(err.data.message)
      })
    },
    dataInit(data){
      data = data || {};
      let { name, address, workTime, deleteFlag, contactPerson, phone, province, city, country } = data;
      this.name = name;
      this.address = address;
      this.workTime = GetDate(workTime);
      this.contactPerson = contactPerson;
      this.phone = phone;
      if(!deleteFlag) this.deleteFlag = '0';
      this.defaultAddress = {
        province,
        city,
        country
      }
    },
    submit(){
      let beginTime = Dateformat(this.workTime[0]);
      let endTime = Dateformat(this.workTime[1]);
      let code = this.code;
      let codeId = this.codeId;
      let deleteFlag = null;
      if(parseInt(this.deleteFlag)){
        deleteFlag = true;
      }else{
        deleteFlag = false;
      }
      let params = {
        name: this.name,
        contactPerson: this.contactPerson,
        address: this.address,
        workTime: beginTime && endTime ? `${beginTime} - ${endTime}` : '',
        phone: this.phone,
        deleteFlag,
      }
      let addBranchAddress = this.$refs.addBranchAddress.getData();
      let param = this.lodash.assign({}, params, addBranchAddress);
      if( code === "0" ) param.physicalId = codeId;
      if( code === "1" ) param.chinaMedId = codeId;
      if( code === "2" ) param.dentistryId = codeId;
      this.check(param, requireArray).then(()=>{ 
        this.addUser(param) 
      },()=>{
        this.$message(this.checkTip) 
      }).catch((error)=>{ 
        this.$message(error) 
      });
    },
    editUser(param){ //修改分院信息
      param.id = this.id;
      this.api.put(urls[this.code], param, 'form').then((res)=>{
        this.$router.go(-1);
      }).catch((err) => {
        console.log(err)
        this.$message(err.data.message)
      })
    },
    addUser(param){
      if(this.$route.query.type === 'edit') return this.editUser(param);
      this.api.post(urls[this.code], param, 'form').then((res)=>{
        this.$router.go(-1);
      }).catch((err) => {
        this.$message(err.data.message)
      })
    },
    cancle(){
      this.$router.go(-1)
    }
  },
  components: {
    UserAddress
  }
}
</script>
 
<style lang="less" scoped>
.box{
  padding-left: 20px;
  h3{
    margin-top: 10px;
    margin-bottom: 20px;
  }
}
.input-box {
  display: flex;
  align-items: center;
  margin-bottom: 15px;
  margin-left: 10px;
  &.box-last{
    margin-left: 30px;
    margin-top: 20px;
  }
}
.label{
  width: 80px;
  min-width: 80px;
  margin-left: 20px;
  &.require{
    position: relative;
    &::before{
        content: '*';
        color: red;
        position: absolute;
        left: -10px;
        top: 4px;
    }  
  }
}
.label-left{
  margin-left: 15px;
}
.input-width{
  width: 216px;
  &.address{
    width: 690px;
  }
}
.el-textarea{
  width:300px;
}
.el-select{
  margin-right: 10px;
}
.avatar-uploader{
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
 